package fyp.spedmew.connector;

import android.os.AsyncTask;
import android.util.Log;

import java.io.IOException;

import fyp.spedmew.backend.entity.userEndpoint.model.User;


/**
 * Created by Andy on 22/12/2014.
 */
public class UserRegistrationATask extends AsyncTask<User, Void, String> {
    private static final String TAG = new Object(){}.getClass().getCanonicalName();
    public static final String USER_EXIST = "1";
    public static final String OK = "0";

    @Override
    protected String doInBackground(User... params) {
        try {
            User toCreate = params[0];
            toCreate.setName(toCreate.getName().toLowerCase());
            User checkExist = ApiService.getUserEndpoint().findByUsername(toCreate.getName()).execute();
            if (checkExist != null) {
                return USER_EXIST;
            }
            ApiService.getUserEndpoint().insertUser(toCreate).execute();
        } catch (IOException e) {
            Log.e(TAG, Log.getStackTraceString(e));
        }
        return OK;
    }
}